function logout(){
	$.post("logout",function(data){
		$("#weibo").html("");
	},"json");
};
function pichidden(pic1,pic2){
	$("#"+pic1).attr("style","display:none");
	$("#"+pic2).attr("style","display:");
};
function showUserInfo(name){
	cleardiv();
	$("#userinfo").attr("style","display:");
	$.post("sina/userinfo",{"screenName":name},function(data){
		var success = data.success;	
		if(success == 0){
			var screenName = data.screenName;
			var verified = data.verified;
			var location = data.location;
			var friendsCount = data.friendsCount;
			var followersCount = data.followersCount;
			var statusesCount = data.statusesCount;
			var profileImageUrl = data.profileImageUrl;
			if(verified){
				$("#screenName").html(screenName+"<img src=\"http://img.t.sinajs.cn/t4/style/images/common/transparent.gif\">");
			}else{
				$("#screenName").html(screenName);
			}
			$("#location").html(location);
			$("#friendsCount").html(friendsCount);
			$("#followersCount").html(followersCount);
			$("#statusesCount").html(statusesCount);				
			$("#profileImageUrl").attr("src",profileImageUrl);
		}
	},"json");
};
function showmyweibo(pageNum,name){
	//showUserInfo(name);
	$.post("sina/showweibo",{"pageNum":pageNum,"screenName":name},function(data){
		var success = data.success;			
		if(success == 1){
			var login_url = data.login_url;
			//登陆
			$("#weibo").html("<a href=\""+login_url+"\">登陆</a>");
		}
		else{
			var weibos = data.weibo;
			var previousCursor = data.previousCursor;
			var nextCursor = data.nextCursor;
			var totalNumber = data.totalNumber;
			
			cleardiv();
			$("#userinfo").attr("style","display:");
			var num = weibos.length;				
			for(var i = 0; i < num; i++){
				var uid = weibos[i].uid;		
				var text = weibos[i].text;					
				var name = weibos[i].name;
				var verified = weibos[i].verified;
				var source = weibos[i].source;
				var thumbnailpic = weibos[i].thumbnailpic;//缩略图
				var originalPic = weibos[i].originalPic;//原始图
				var retweetedstatus = weibos[i].retweetedstatus;
				var repostsCount = weibos[i].repostsCount;
				var commentsCount = weibos[i].commentsCount;
				var profileImageUrl = weibos[i].profileImageUrl;
				$("#weibo").append("<dl class=\"feed_list W_linecolor\">");
				$("#weibo").append("<dt class=\"face\">");					
				$("#weibo").append("<img src=\""+profileImageUrl+"\">");
				$("#weibo").append("</dt>");	
				
				$("#weibo").append("<dd class=\"content\">");
				
				$("#weibo").append("<a id=\"javascript\" href=\"javascript:showmyweibo(1,'"+name+"')\" style=\"text-decoration:none\">"+name+"</a>:");					
				if(verified){
					$("#weibo").append("<img src=\"http://img.t.sinajs.cn/t4/style/images/common/transparent.gif\" height=\"12px\" width=\"14px\">");
				}					
				$("#weibo").append("<br>");
				$("#weibo").append(text);
				if(retweetedstatus != null){
					$("#weibo").append("<br>");
					$("#weibo").append("*************");
					$("#weibo").append("<br>");
					var st = retweetedstatus;
					$("#weibo").append(st.text);
					$("#weibo").append("<br>");
					$("#weibo").append("*************");
					$("#weibo").append("<br>");
				}
				$("#weibo").append("<br>");
				if(thumbnailpic != ""){
					$("#weibo").append("<img id='picthumbnailpic"+i+"' src='"+thumbnailpic+"'>");
					$("#weibo").append("<img id=\"picoriginalPic"+i+"\" src=\""+originalPic+"\">");
					$("#picoriginalPic"+i).attr("style","display:none");
					
					$("#picthumbnailpic"+i).attr("onclick","javascript:pichidden(\"picthumbnailpic"+i+"\",\"picoriginalPic"+i+"\")");
					$("#picoriginalPic"+i).attr("onclick","javascript:pichidden(\"picoriginalPic"+i+"\",\"picthumbnailpic"+i+"\")");
				}
				$("#weibo").append("<br>");
				$("#weibo").append("来自:"+source);
				$("#weibo").append("<br>");
				$("#weibo").append("<a href=\"\" style=\"text-decoration:none\">转发("+repostsCount+")</a>|");	
				$("#weibo").append("<a href=\"\" style=\"text-decoration:none\">收藏</a>|");
				$("#weibo").append("<a href=\"\" style=\"text-decoration:none\">评论("+commentsCount+")</a>");
				
				
				$("#weibo").append("</dd>");
				$("#weibo").append("</dl>");
			}
			$("#weibo").append("<br>");
			$("#weibo").append("<br>");
			//上一页
			if(pageNum > 1){
				$("#weibo").append("<a href=\"javascript:showmyweibo("+(pageNum-1)+",'')\">上一页</a>&nbsp");
			}
			var num = Math.floor(totalNumber/20);

			if(totalNumber%20 > 0){
				num += 1;
			}
			var pageNumSize = 8;
			if(num <= 8){
				pageNumSize = num;
			}
			//下一页
			if(pageNum <num){
				$("#weibo").append("<a href=\"javascript:showmyweibo("+(pageNum+1)+",'')\">下一页</a>");
			}
			
			//显示
			
		}
	},"json");
};

function showmyfriends(pageNum){
	cleardiv();
	if(pageNum == null){
		pageNum = 0;
	}
	$.post("sina/myfriends",{"count":20,"pageNum":pageNum},
		function(data){
		var success = data.success;
		if(success == 0){
			var nextCursor = data.nextCursor;
			var previousCursor = data.previousCursor;
			var totalNumber = data.totalNumber;
			//成功
			var myfriends = data.myfriends;
			var num = myfriends.length;
			var show_friends_str = "";
			for(var i = 0; i < num; i++){
				var name = myfriends[i].name;//名字
				var location = myfriends[i].location;//地址
				var friendsCount = myfriends[i].friendsCount;//关注数
				var followersCount = myfriends[i].followersCount;//粉丝数
				var statusesCount = myfriends[i].statusesCount;//微博数
				var description = myfriends[i].description;//简介
				var followMe = myfriends[i].followMe;//是否关注我
				show_friends_str += "<div>";
				show_friends_str += name;
				show_friends_str += " "+location;
				if(followMe){
					//相互
					show_friends_str +="&nbsp &nbsp &nbsp &nbsp &nbsp";						
					show_friends_str +="相互关注";
				}	
				
				show_friends_str +="<br>";
				show_friends_str +="关注:"+friendsCount;
				show_friends_str +="|粉丝:"+followersCount;
				show_friends_str +="|微博:"+statusesCount;
				show_friends_str +="<br>";
				show_friends_str +="简介:"+description;					
								
				show_friends_str +="</div>";
				show_friends_str +="--------------------------------------------------------";
				show_friends_str +="<br>";				
			}
			
			
			//if(nextCursor<totalNumber){
			var num = Math.floor(totalNumber/20);

			if(totalNumber%20 > 0){
				num += 1;
			}
			var pageNumSize = 8;
			if(num <= 8){
				pageNumSize = num;
			}
			
			if(pageNum > 1){
				show_friends_str +="<a href=\"javascript:showmyfriends("+(pageNum-1)+")\">上一页</a>&nbsp";
			}
			
			for(var i = 0;i < pageNumSize; i++){
				if(pageNum == (i+1)){
					show_friends_str +="<a>"+(i+1)+"</a>";
				}else{
					show_friends_str +="<a href=\"javascript:showmyfriends("+(i+1)+")\">"+(i+1)+"</a>";
				}	
				show_friends_str +="&nbsp";
			}
			if(pageNum <num){
				show_friends_str +="<a href=\"javascript:showmyfriends("+(pageNum+1)+")\">下一页</a>";
			}
			
			show_friends_str +="<br>";
			//}
			
			$("#showmyfriend").append(show_friends_str);
		}else{
			var login_url = data.login_url;
			$("#showmyfriend").append("<a href=\""+login_url+"\">"+login_url+"</a>");				
		}
	},"json");
};


function showdazahui(pageNum){
	
}

function cleardiv(){	
	$("#weibo").html("");
	$("#showmyfriend").html("");
	$("#userinfo").attr("style","display:none");
}


$(function(){
	showUserInfo();
});